using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System;

namespace MyUtils
{
#if NET4

	public class DynamicDictionaryObject : DynamicObject, IDictionaryObject
	{
		readonly DictionaryObject _dic;
		public DynamicDictionaryObject(DictionaryObject dic)
		{
			_dic = dic;
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			result = _dic[binder.Name];
			return true;
		}

		public override bool TrySetMember(SetMemberBinder binder, object value)
		{
			_dic[binder.Name] = value;
			return true;
		}

		#region Implementation of IDictionaryObject

		public object this[string propertyName]
		{
			get { return _dic[propertyName]; }
			set { _dic[propertyName]=value; }
		}

		public T Get<T>(string propertyName)
		{
			return (T)this[propertyName];
		}

		public T Get<T>(string propertyName, Func<T> factory) where T : class
		{
			return Get<T>(propertyName) ?? Set(propertyName, factory());
		}

		public T Set<T>(string propertyName, T value)
		{
			this[propertyName] = value;
			return value;
		}

		#endregion
	}

#endif
}